<!DOCTYPE html>
<html>

<head>
    <head/>
    <title>我的预约</title>
    <titleType style="display: none">wdyyList</titleType>
    <%- include('./_global/appendJs') %>
        <style type="text/css">
            #orderUl li {
                background: #fff;
            }

            #orderUl li:first-child {
                margin-top: 0 !important;
            }

            #orderUl .cancelButton {
                border: 1px solid #ccc;
                padding: .5rem;
                border-radius: .2rem;
            }

            .item-content span[name="orderId"] {
                width: 50%;
                display: inline-block;
                position: relative;
                overflow: hidden;
                text-overflow: ellipsis;
                vertical-align: middle;
            }

            textarea {
                border: 1px solid #666;
                border-radius: 5px;
                text-indent: 8px;
            }

            .page-user .user-title {
                display: inline-block;
            }
        </style>
</head>

<body>
    <section class="page-wrap page-user">
        <!-- header start -->
        <!-- <header class="header-box grey fixed">
            <div class="header-content">
                <div class="left-tool">
                    <a class="back" href="javascript:void(0);">
                        <i class="iconfont icon-back"></i>
                    </a>
                </div>
                <div class="title">我的预约</div>
            </div>
            <div class="right-tool">
                <a class="tool-a" onclick="toSelfCenter()">返回首页</a>
            </div>
        </header> -->
        <!-- header end -->

        <!--内容-->
        <div class="plane-box first" id="orderDiv" style="background: #f5f7fc">
            <ul class="default-list" id="orderUl">
            </ul>
        </div>
        <!--内容END-->
    </section>
</body>
<script type="text/html" id="orderLiTop">
    <li style="margin-top:.5rem ">
        <div class="item-inner">
            <div class="item-content">
                <span class="user-title">就诊人:</span>
                <span class="user-name orderInfo" name="patientName">张三</span>
            </div>
            <div class="item-after">
                <span class="check orderInfo" name="orderStatus">预约成功</span>
            </div>
        </div>
    </li>
</script>
<script type="text/html" id="orderLiDown">
    <li>
        <div class="item-inner big-box">
            <div class="item-content">
                <p class="doc">
                    <span class="orderInfo" name="docName"></span>
                    <span class="second orderInfo" name="docTitle"></span>
                </p>
                <p class="doc">
                    <span>就诊医院：</span>
                    <span class="second orderInfo" name="hosName">上海第九医院（风湿免疫科）</span>
                </p>
                <!--  <p class="doc"><span>门诊费用：</span><span class="second orderInfo" name="visitCost">普通门诊14:00元（支付宝）</span></p> -->
                <!--      <p class="doc"><span>门诊类型：</span><span class="second orderInfo" name="regType1">普通门诊</span></p> -->
                <p class="doc">
                    <span>就诊时间：</span>
                    <span class="second orderInfo" name="jzsj">2017-02-28</span>
                </p>
            </div>
            <div class="item-after">
                <i class="iconfont icon-more"></i>
            </div>
        </div>
    </li>
</script>
<script type="text/html" id="orderNum">
    <li>
        <div class="item-inner">
            <div class="item-content">
                <span class="user-title">订单编号:</span>
                <span class="user-name orderInfo" name="orderId">13456732</span>

            </div>
            <div class="item-after ">
                <span class="check orderInfo cancelButton" name="orderStatus">取消预约</span>
            </div>
        </div>
    </li>
</script>

<script src="/static/js/gzd/pageUrl.js"></script>
<script src="/quickJs/scripts/swiper/3.4.2/js/swiper.min.js"></script>
<script type="text/javascript">
    /*订单状态*/
    var tb_zy_order_status = {
        "1": "已预约",
        "2": "已退号",
        "3": "已就诊",
        "4": "其他",
        "5": "其他",
        "6": "其他"
    };
    var requestRoot = "/api/gzd/yygh/";
    // var yyghUserInfo = StoreCache.getCache("yyghUserInfo");
    var userCardId = $.QuickUrlUtils.getRequest("userCardId");
    var userCardType = $.QuickUrlUtils.getRequest("userCardType");


    /**查看详情*/
    function toYydetail(data) {
        var info = data;
        StoreCache.setCache("YYGH_MEDICARD", {
            mediCardId: info["mediCardId"],
            password: info["password"]
        });
        StoreCache.setCache("YYGH_ORDERINFO", {
            orderId: info["orderId"]
        });
        $.pageSwitch.goToUrl("myOrder", "yyDetail", "orderId=" + info.orderId + "&hosCode=" + info.hosCode +
            "&oneDepCode=" + info.oneDepCode);
    }

    function cancelOrder(data) {

        $.QuickAlert.alertConfirm({
            content: "<div style='margin:-38px 0px -25px 0px;'><div style='float:left;margin-top:30px;'>退号原因：</div><textarea  id=\"cancelReason\" rows='3' cols='20'></textarea></div>",
            title: "确认是否退号",
            callback: function (cancelReason) {
                // var data = StoreCache.getCache("yyInfo");    

            },
            yes: function (index) {

                if (StringUtils.isEmpty($("#cancelReason").val())) {
                    $.QuickAlert.alertNormal("请填写退号原因！", {
                        callback: function (indexAlert) {
                            layer.close(indexAlert);
                        }
                    });

                } else {
                    var cancelReason = $("#cancelReason").val();

                    var params = {
                        orderId: data.orderId,
                        hosOrgCode: data.hosCode,
                        cancelReason: cancelReason
                    };
                    // $.QuickRemote.AjaxJson({
                    //     funcName:requestRoot + "cancelReservation"
                    //     ,callback: function(result){
                    //         var message =data.orderStatus=="1"? "取消预约成功！":"退号成功！";
                    //         $.QuickAlert.alertNormal(message,{callback: function(){
                    //             /**刷新当前页面*/
                    //             location.reload();

                    //         }});
                    //     },params:params
                    // });
                    $.ajax({
                        type: "POST", // 使用get方法访问后台
                        dataType: "json", // 返回json格式的数据
                        "headers": {
                            "Content-Type": "application/json",
                            "Cache-Control": "no-cache",
                        },
                        url: "/api/yyghAdapter/cancelReservation", // 要访问的后台地址
                        data: JSON.stringify(params),
                        success: function (result) {
                            if (result.code == "SUCCESS") {
                                // if (data && data.data) {
                                // var message = data.data.orderStatus == "1" ? "取消预约成功！" :
                                // "退号成功！";
                                $.QuickAlert.alertNormal("取消预约成功！", {
                                    callback: function () {
                                        var isReg = data["orderType"] == "1" ? ( data.depName + " " + "专家门诊 " + " " + "(" + data.docName + "医生)" ) : (  data.depName+ " " + "普通门诊 " );

                                        var sendMsg = {
                                            phone : data.patientPhone,
                                            // phone : "18501614744",
                                            content : "【"+ data.hosName +"】"+ data.patientName +",您预约的"+ data.scheduleDate +", " + "科室名称" + " " + isReg  + " 就诊时间为" + data.startTime + " - " + data.endTime +" 的号源已取消，谢谢您的支持。祝您生活愉快!"
                                        };
                                        
                                        // 发送短信
                                        $.ajax({
                                            type: "POST", // 使用get方法访问后台
                                            dataType: "json", // 返回json格式的数据
                                            "headers": {
                                                "Content-Type": "application/json",
                                                "Cache-Control": "no-cache",
                                            },
                                            url: "/api/yyghAdapter/sendMsg", // 要访问的后台地址
                                            data: JSON.stringify(sendMsg),
                                            success: function (data) {
                                                if (data.code == "SUCCESS") {
                                                    // debugger;
                                                        location.reload();
                                                    
                                                } else {
                                                    $.QuickAlert.alertFail({
                                                        content: data.message
                                                    });
                                                }
                                            }
                                        });


                                    }
                                });
                                // }
                            } else {
                                $.QuickAlert.alertFail({
                                    content: data.message
                                });
                            }
                        }
                    });
                }

            }


        });




    }

    var page_wdyy = (function ($, page_wdyy) {

        /**加载视图*/
        function initView() {

        }

        /**加载数据*/
        function initData() {
            page_wdyy.initOrderList();
        }

        /**查询预约单列表*/
        function initOrderList() {
            $("#orderUl").remove();
            var orderUl = $("<ul>").attr({
                "id": "orderUl",
                "class": "default-list"
            });
            $("#orderDiv").append(orderUl);
            var nowdate = new Date();
            nowdate.setMonth(nowdate.getMonth() - 1);
            var lastMonth = nowdate.Format("yyyy-MM-dd");
            nowdate.setMonth(nowdate.getMonth() + 2);
            var nextMonth = nowdate.Format("yyyy-MM-dd");
            var params = {
                startTime: lastMonth,
                endTime: nextMonth,
                queryDateType: "1",
                userCardId: userCardId,
                userCardType: userCardType,
                queryPersonType: "2"
            }
            // $.QuickRemote.AjaxJson({
            //     funcName: $.getServiceUrl.getUrl("getOrderRecords"),
            //     callback: page_wdyy.getOrderRecordsHandler,
            //     params: params
            // });
            $.ajax({
                type: "POST", // 使用get方法访问后台
                dataType: "json", // 返回json格式的数据
                "headers": {
                    "Content-Type": "application/json",
                    "Cache-Control": "no-cache",
                },
                url: "/api/yyghAdapter/getOrderRecords", // 要访问的后台地址
                data: JSON.stringify(params),
                success: function (data) {
                    if (data.code == "SUCCESS") {
                        if (data && data.data) {
                            page_wdyy.getOrderRecordsHandler(data.data);
                        }
                    } else {
                        $.QuickAlert.alertFail({
                            content: data.message
                        });
                    }
                }
            });

        }

        /**解析结果创建预约单列表*/
        function getOrderRecordsHandler(result) {
            var orders = result["orders"];
            if (orders && orders.length > 0) {
                orders.sort(page_wdyy.compare('orderId'));
                page_wdyy.createItems(orders);
            } else {
                // $.QuickAlert.alertFail({
                //     content: "没有预约信息！"
                // });
                $("#orderDiv").append(
                    "<div style='width:100%;text-align:center; font-weight:bold;color:#000;padding-top:20px;padding-bottom:20px'>没有预约信息</div>"
                );

                $("#orderDiv").removeClass("plane-box");
            }
        }

        function compare(property) {
            return function (a, b) {
                var value1 = a[property].stringToDate();
                var value2 = b[property].stringToDate();
                return value2 - value1;
            }
        }

        function createItems(ary) {
            var lis = [];
            $.each(ary, function (index, item) {
                var orderLiTop = $($("#orderLiTop").html());
                var orderLiDown = $($("#orderLiDown").html());
                var orderNum = $($("#orderNum").html());
                item["jzsj"] = item["scheduleDate"] + " " + item["startTime"] + "-" + item["endTime"];

                $(orderLiTop).find(".orderInfo").each(function () {
                    if ($(this).attr("name") == "orderStatus") {
                        var status = item['orderStatus'];
                        $(this).text(status == '' ? '' : tb_zy_order_status[status]);
                    } else {
                        $(this).text(item[$(this).attr("name")]);
                    }

                });
                $(orderLiDown).find(".orderInfo").each(function () {
                    if ($(this).attr("name") == "hosName") {
                        $(this).text(item.hosName + "(" + item.depName + ")");
                    } else if ($(this).attr("name") == "visitCost") {
                        if (!StringUtils.isEmpty(item.visitCost)) {
                            $(this).text(parseInt(item.visitCost).toFixed(2));
                        } else {
                            $(this).text("");
                        }


                    } else {
                        $(this).text(item[$(this).attr("name")]);
                    }
                });
                $(orderLiDown).find(".item-after").attr("onclick", "toYydetail(" + JSON.stringify(item) +
                    ")");
                $(orderNum).find(".orderInfo").each(function () {
                    if ($(this).attr("name") == "orderStatus") {
                        if (item[$(this).attr("name")] == "1") {
                            $(this).text("取消预约");
                            $(this).attr("onclick", "cancelOrder(" + JSON.stringify(item) + ")");
                            $(this).show();
                        } else if (item[$(this).attr("name")] == "2") {
                            $(this).hide();
                            /*  $(this).text("退号");
                              $(this).attr("onclick","cancelOrder("+JSON.stringify(item)+")");*/
                            //  $(this).show();
                        } else {
                            $(this).hide();
                        }
                    } else {
                        $(this).text(item[$(this).attr("name")]);
                    }

                });
                lis.push(orderLiTop);
                lis.push(orderLiDown);
                lis.push(orderNum);
            });
            $("#orderUl").append(lis);
        }

        /**获取门诊类型*/
        function getRegType(regType) {
            var value = "";
            if (regType == "100") {
                value = "普通门诊";
            } else if (regType == "101") {
                value = "专科门诊";
            } else if (regType == "102") {
                value = "专家门诊";
            } else if (regType == "103") {
                value = "特需门诊";
            } else if (regType == "104") {
                value = "专病门诊";
            } else if (regType == "999") {
                value = "其它门诊";
            }
            return value;
        }

        var cfg_def = {
            initData: initData,
            initView: initView,
            initOrderList: initOrderList,
            getOrderRecordsHandler: getOrderRecordsHandler,
            compare: compare,
            createItems: createItems,
            getRegType: getRegType,
        }

        page_wdyy.init = function (cfg) {
            $.extend(page_wdyy, cfg_def, cfg);
            page_wdyy.initData();
            page_wdyy.initView();
        }

        return page_wdyy;
    })(jQuery, {});
</script>

</html>